package com.hl.web;

import com.hl.pojo.Answers;
import com.hl.pojo.Users;
import com.hl.service.WodehuidaService;
import com.hl.util.GeneralResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/wodehuida")
public class WodehuidaController {
    @Autowired
    private WodehuidaService wodehuidaService;

    @RequestMapping("/answer")
    public GeneralResult getAnswersById(@RequestBody Map<String, Integer> id) {
        try {
            Integer userId = id.get("userId");
            if (userId == 0) {
                // 处理无效的userId，例如返回错误结果
                return GeneralResult.fail();
            }
            return GeneralResult.success(wodehuidaService.getAnswers(userId));
        } catch (NumberFormatException e) {
            // 处理userId无法转换为Integer的情况
            return GeneralResult.fail();
        }
    }

    //查询全部
    @RequestMapping("/answerAll")
    public GeneralResult getAnswersListAll(@RequestBody Map<String,String> map) {
        String answerContent =  map.get("answerContent");
        String userName = map.get("userName");

        return GeneralResult.success(wodehuidaService.AnswersListAll(userName,answerContent));
    }

    //根据id删除
    @RequestMapping("/answerDelete")
    public GeneralResult getAnswersDelete(@RequestBody Map<String, Integer> map) {
        Integer answerId = map.get("answerId");
        return GeneralResult.success("请求成功", wodehuidaService.deleteAnswers(answerId));
    }

    //新增数据回答
    @RequestMapping("/addAnswer")
    public GeneralResult addAnswersOne(@RequestBody Map<String, Object> map) {
        Integer userId = (Integer) map.get("userId");
        String answerContent = (String) map.get("answerContent");
        Answers answers = new Answers();
        answers.setUserId(userId);
        answers.setAnswerContent(answerContent);
        int num = wodehuidaService.addAnswers(answers);
        return GeneralResult.success("请求成功", num);
    }

    //查询用户
    @RequestMapping("/searchUser")
    public GeneralResult searchUserList() {
        return GeneralResult.success(wodehuidaService.searchUserList());
    }

    //    更改回答表
    @RequestMapping("/updateAnswers")
    public GeneralResult updateAnswers(@RequestBody Map<String,String> map) {
        Integer answerId = Integer.valueOf(map.get("answerId"));
        String answerContent = map.get("answerContent");
        Answers answers = new Answers();
        answers.setAnswerId(answerId);
        answers.setAnswerContent(answerContent);
        return GeneralResult.success("请求成功",wodehuidaService.updateUserAndAnswer(answers));
    }

    //    更改用户信息
    @RequestMapping("/updateUsers")
    public GeneralResult updateUsers(@RequestBody Map<String,String> map) {
        String userName = map.get("userName");
        String phoneNumber = map.get("phoneNumber");
        Users users = new Users();
        users.setUserName(userName);
        users.setPhoneNumber(phoneNumber);
        System.out.println(wodehuidaService.updateUser(users));
        return GeneralResult.success("请求成功",wodehuidaService.updateUser(users));
    }


}
